﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Globalization;

namespace Statistic
{
    sealed public class CareerReader : IReader 
    {
        int i = 0;

        /**
 * Cleans htmlcode
 * @param url
 *  the html code to be cleaned
 * @param first
 *  from..
 * @param second
 *   to..
 * @return result_
 *   The html code cleaned
 */
        public static string CleanSource(string source, string first, string second)
        {
            string cleanedSource = "";
            try
            {
                string SearchString = first;
                int FirstChr = source.IndexOf(SearchString);
                string SearchString1 = second;
                int FirstChr1 = source.IndexOf(SearchString1);
                int yo = FirstChr1 - FirstChr;
                cleanedSource = source.Substring(FirstChr, yo);
            }
            catch (ArgumentOutOfRangeException)
            {
                cleanedSource = "";
            }
            return cleanedSource;
        }

        /**
        * Converts date to the correct format
        * @param result
        * @return result
        *  correct date format
        */
        private static DateTime GetDate(string result, string message)
        {         
            Thread.CurrentThread.CurrentCulture = new CultureInfo("el-GR", true);
            string url = "";
            int FirstChr = result.IndexOf(message);
            DateTime dateTime;
            //!!
            try
            {
                int FirstChr1 = result.Length - 1;
                int yo = FirstChr1 - FirstChr;
                string strTemp = result.Substring(FirstChr, yo);
                url = CleanSource(strTemp, "<div>", "</div>");
                result = url.Remove(0, 5);
                dateTime = DateTime.Parse(result);
            }
            catch (ArgumentOutOfRangeException)
            {
                dateTime = DateTime.Now;              
            }
            catch (FormatException)
            {
                dateTime = DateTime.Now;
            }
            return dateTime;
        }

        /**
         * Get all advertisement links
         * @return advs
         *   A list which contains all advertisement links
         */
        IList<string> IReader.GetLinks()
        {
            Protocol statsProtocol = new Protocol();

            int a = 1;
            IList<string> advs = new List<string>();
            bool continueSearch = true;
            string htmlCode;

            while (continueSearch)
            {
                i++;
                for (int k = 1; k < 26; k++)
                {
                    htmlCode = statsProtocol.GetHtmlCode("http://www.kariera.gr/GR/JobSeeker/Jobs/JobResults.aspx?IPath=JRTCM&excrit=QID%3dA3853799130912%3bst%3dA%3buse%3dALL%3bCID%3dGR%3bSID%3d%3f%3bTID%3d0%3bENR%3dNO%3bDTP%3dDRNS%3bYDI%3dYES%3bIND%3dALL%3bPDQ%3dAll%3bJN%3dJN008%3bPAYL%3d0%3bPAYH%3dGT120%3bPOY%3dNO%3bETD%3dALL%3bRE%3dALL%3bMGT%3dDC%3bSUP%3dDC%3bFRE%3d30%3bCHL%3dIL%3bQS%3dSID_UNKNOWN%3bSS%3dNO%3bTITL%3d0%3bOB%3d-modifiedint%3bRAD%3d30%3bJQT%3dRAD%3bJDV%3dFalse%3bExpHigh%3dGT50%3bExpLow%3d0%3bMaxLowExp%3d-1&pg=" + i + "&sc_cmp2=JS_GR_JRP_Jumpto_1");
                    string searchString = "J_D__ctl" + k + "_cblTitle";

                    int FirstChr = htmlCode.IndexOf(searchString);
                    if (FirstChr == -1)
                    {
                        break;
                    }
                    int lastChr = htmlCode.Length - 1;
                    int yo = lastChr - FirstChr;
                    string cuttedHtmlCode = htmlCode.Substring(FirstChr, yo);
                    string cleanedHtml = CleanSource(cuttedHtmlCode, "http", "\"" + ">");
                    advs.Add(cleanedHtml);
                }
                a++;
                htmlCode = statsProtocol.GetHtmlCode("http://www.kariera.gr/GR/JobSeeker/Jobs/JobResults.aspx?IPath=JRTCM&excrit=QID%3dA3853799130912%3bst%3dA%3buse%3dALL%3bCID%3dGR%3bSID%3d%3f%3bTID%3d0%3bENR%3dNO%3bDTP%3dDRNS%3bYDI%3dYES%3bIND%3dALL%3bPDQ%3dAll%3bJN%3dJN008%3bPAYL%3d0%3bPAYH%3dGT120%3bPOY%3dNO%3bETD%3dALL%3bRE%3dALL%3bMGT%3dDC%3bSUP%3dDC%3bFRE%3d30%3bCHL%3dIL%3bQS%3dSID_UNKNOWN%3bSS%3dNO%3bTITL%3d0%3bOB%3d-modifiedint%3bRAD%3d30%3bJQT%3dRAD%3bJDV%3dFalse%3bExpHigh%3dGT50%3bExpLow%3d0%3bMaxLowExp%3d-1&pg=" + a + "&sc_cmp2=JS_GR_JRP_Jumpto_1");
                continueSearch = HtmlString.PageExists(htmlCode, "Συγγνώμη, δε βρέθηκαν αποτελέσματα που να ταιριάζουν με τα κριτήρια που θέσατε.");
            }
            return advs;
        }

        /**
         * Creates a list with advertisement objects 
         *  and write them to the xml
         */
        IList<Advertisements> IReader.DoTheWork()
        {
            Protocol statsProtocol = new Protocol();
            IList<string> advs = new List<string>();
            IList<Advertisements> advsResult = new List<Advertisements>();
            IReader reader = new CareerReader();
            advs = reader.GetLinks();

            for (int i = 0; i < advs.Count; i++)
            {
                Advertisements advertisements = new Advertisements();
                string htmlCode = statsProtocol.GetHtmlCode(advs[i].ToString());
                string cleanedhtmlCode = CleanSource(htmlCode, "<!-- Description Content -->", "<!-- End Description Content -->");
                cleanedhtmlCode = HtmlString.StripTagsCharArray(cleanedhtmlCode);
                advertisements.Content = cleanedhtmlCode;
                DateTime date = GetDate(htmlCode, "Τελευταία ανανέωση:");
                advertisements.Date = date;
                advsResult.Add(advertisements);
            }
            return advsResult;
        }
    }
}
